//
//  PracticingView.m
//  HotWord
//
//  Created by Rainbow on 12-11-23.
//
//

#import "PracticingView.h"
#import "Meaning.h"
#import "CacheData.h"

@interface PracticingView()
{
    NSTimer *_timer;
    NSInteger _timeCount;
}

- (void)hideView:(UIView *)view withAnimation:(BOOL)animated;
- (void)showView:(UIView *)view withAnimation:(BOOL)animated;
- (void)hideAllViews;

@end

@implementation PracticingView

- (void)addTranslateScroll
{
    [self showView:self.answerScrollView withAnimation:YES];
    [self.answerScrollView removeAllSubviews];
    [self.answerScrollView setContentOffset:CGPointZero];
    NSInteger meaningCount = [_word.meanings count];
    for (int i = 0; i < meaningCount; i++) {
        Meaning *meaning = [_word.meanings objectAtIndex:i];
        UILabel *label = [self newLabelWithText:meaning.chineseTranslate];
        label.left = 0 + i*_answerScrollView.width;
        [_answerScrollView addSubview:label];
    }
    if (meaningCount <= 1) {
        
        [self hideView:self.leftArrowView withAnimation:YES];
        [self hideView:self.rightArrowView withAnimation:YES];
    }else {
        [self hideView:self.leftArrowView withAnimation:YES];
        [self showView:self.rightArrowView withAnimation:YES];
    }
    
    [self.answerScrollView setContentSize:CGSizeMake(self.answerScrollView.width * meaningCount, self.answerScrollView.height)];
}

- (UILabel *)newLabelWithText:(NSString *)meaning
{
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)] autorelease];
    label.text = meaning;
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 0;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

- (void)setWordData:(Word *)word
{
    if (!word) {
        [self hideAllViews];
        return;
    }
    self.answerScrollView.delegate = self;
    self.endSessionView.alpha = 0;
    self.word = word;
    [self hideView:self.leftArrowView withAnimation:YES];
    [self hideView:self.rightArrowView withAnimation:YES];
    [self showView:self.contentLabel withAnimation:YES];
    _contentLabel.text = _word.content;
    [self hideView:self.wrongBtn withAnimation:YES];
    [self hideView:self.rightBtn withAnimation:YES];
    [self showView:self.answerBtn withAnimation:YES];
    [self hideView:self.lockImageView withAnimation:YES];
    [self hideView:self.timeLabel withAnimation:YES];
    [self hideView:self.tryAgainLabel withAnimation:YES];
    _nextLabel.text = @"CHECK";
    [self hideView:self.answerScrollView withAnimation:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentSize.width > scrollView.width) {
        if (scrollView.contentOffset.x <= 0) {
            [self hideView:self.leftArrowView withAnimation:YES];
            [self showView:self.rightArrowView withAnimation:YES];
        }
        else if (scrollView.contentOffset.x >= scrollView.contentSize.width - scrollView.width) {
            
            [self showView:self.leftArrowView withAnimation:YES];
            [self hideView:self.rightArrowView withAnimation:YES];
        }
        else
        {
            [self showView:self.leftArrowView withAnimation:YES];
            [self showView:self.rightArrowView withAnimation:YES];
        }
    }
}

- (void)dealloc {
    if (_timer) {
        [_timer invalidate];
    }
    [_word release];
    [_contentLabel release];
    [_wrongBtn release];
    [_rightBtn release];
    [_answerBtn release];
    [_lockImageView release];
    [_timeLabel release];
    [_tryAgainLabel release];
    [_nextLabel release];
    [_answerScrollView release];
    [_leftArrowView release];
    [_rightArrowView release];
    [_endSessionView release];
    [_backgroundView release];
    [super dealloc];
}

- (IBAction)tapOnWrongBtn:(id)sender {
    
    [self hideView:self.answerScrollView withAnimation:YES];
    [self showView:self.contentLabel withAnimation:YES];
    [self hideView:self.tryAgainLabel withAnimation:YES];
    self.timeLabel.text = @"20";
    [self showView:self.timeLabel withAnimation:YES];
    self.lockImageView.image = [UIImage imageNamed:@"lock.png"];
    [self showView:self.lockImageView withAnimation:YES];
    [self hideView:self.wrongBtn withAnimation:YES];
    [self hideView:self.rightBtn withAnimation:YES];
    [self showView:self.answerBtn withAnimation:YES];
    [self hideView:self.leftArrowView withAnimation:YES];
    [self hideView:self.rightArrowView withAnimation:YES];
    _nextLabel.text = @"CHECK";
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
    _timeCount = 20;
    self.userInteractionEnabled = NO;
    [[CacheData sharedData] addWrongWord:_word];
}

- (void)updateTimer
{
    _timeCount--;
    if (_timeCount > 0) {
        self.timeLabel.text = [NSString stringWithFormat:@"%d", _timeCount];
    }
    else{
        [self hideView:self.timeLabel withAnimation:YES];
        [_timer invalidate];
        _timer = nil;
        self.lockImageView.image = [UIImage imageNamed:@"unlock.png"];
        self.userInteractionEnabled = YES;
    }
}

- (IBAction)tapOnRightBtn:(id)sender {
    [self hideView:self.leftArrowView withAnimation:YES];
    [self hideView:self.rightArrowView withAnimation:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(PracticeViewChooseRight:)]) {
        [self.delegate PracticeViewChooseRight:self];
    }
}

- (IBAction)tapOnQuestionBtn:(id)sender {
    self.tryAgainLabel.text = @"TRY AGAIN";
    [self showView:self.tryAgainLabel withAnimation:YES];
    [self hideView:self.answerBtn withAnimation:YES];
    [self showView:self.wrongBtn withAnimation:YES];
    [self showView:self.rightBtn withAnimation:YES];
    self.nextLabel.text = @"NEXT";
    [self hideView:self.contentLabel withAnimation:YES];
    [self hideView:self.lockImageView withAnimation:YES];
    [self addTranslateScroll];
}

- (void)hideView:(UIView *)view withAnimation:(BOOL)animated
{
    if (animated) {
        
        [UIView animateWithDuration:0.3
                         animations:^{
                             view.alpha = 0;
                         }];
    }else{
        view.alpha = 0;
    }
}

- (void)showView:(UIView *)view withAnimation:(BOOL)animated
{
    if (animated) {
        [UIView animateWithDuration:0.3
                         animations:^{
                             view.alpha = 1;
                         }];
    }else{
        view.alpha = 1;
    }
}

- (void)hideAllViews
{
    for (UIView *view in self.subviews) {
        if (view != self.backgroundView && view != self.endSessionView) {
            [self hideView:view withAnimation:NO];
        }
    }
    [self showView:self.endSessionView withAnimation:YES];
}

@end
